<?php
/**
 * 内容接口类
 * @return string
 * @Obelisk
 */
namespace app\modules\content\controllers;


use app\modules\cn\models\Info;
use app\modules\content\models\Active;
use app\modules\content\models\Banner;
use app\modules\content\models\Classes;
use app\modules\content\models\Content;
use app\modules\content\models\HotActive;
use app\modules\content\models\IeltsSpeak;
use app\modules\content\models\IeltsWriteTag;
use app\modules\content\models\Recommend;
use app\modules\content\models\Teachers;
use app\modules\content\models\Evaluation;
use yii;
//use app\modules\content\models\Video;
use app\modules\content\models\Category;
use app\modules\content\models\CategoryContent;
use app\modules\content\models\CategoryExtend;
use yii\web\Controller;

class ApiController extends Controller
{
    public $enableCsrfValidation = false;

    /**
     * 内容排序
     * @Obelisk
     */

    public function actionSort()
    {
        $id = Yii::$app->request->post('id');
        $sort = Yii::$app->request->post('sort');
        $cate = Yii::$app->request->post('cate');
        if ($cate == 1) {
            foreach ($id as $k => $v) {
                Teachers::updateAll(['sort' => $sort[$k]], "id=$v");
            }
        }
        if ($cate == 2) {
            foreach ($id as $k => $v) {
                Classes::updateAll(['sort' => $sort[$k]], "id=$v");
            }
        }
        if ($cate == 3) {
            foreach ($id as $k => $v) {
                Info::updateAll(['sort' => $sort[$k]], "id=$v");
            }
        }
        if ($cate == 4) {
            foreach ($id as $k => $v) {
                Banner::updateAll(['sort' => $sort[$k]], "id=$v");
            }
        }
        if ($cate == 5) {
            foreach ($id as $k => $v) {
                Active::updateAll(['sort' => $sort[$k]], "id=$v");
            }
        }
        if ($cate == 6) {
            foreach ($id as $k => $v) {
                Recommend::updateAll(['sort' => $sort[$k]], "id=$v");
            }
        }
        if ($cate == 7) {
            foreach ($id as $k => $v) {
                Evaluation::updateAll(['sort' => $sort[$k]], "id=$v");
            }
        }
        if ($cate == 8) {
            foreach ($id as $k => $v) {
                HotActive::updateAll(['sort' => $sort[$k]], "id=$v");
            }
        }
        if ($cate == 9) {
            foreach ($id as $k => $v) {
                IeltsSpeak::updateAll(['sort' => $sort[$k]], "id=$v");
            }
        }
        echo "<script>alert('成功!');history.back();</script>";

    }

    public function actionUpload()
    {
        $path = Yii::$app->params['upSpoken'];
        if(empty($_FILES['file'])){
            die(json_encode(['code'=>0,'message'=>'请选择文件']));
        }
        $file = $_FILES['file'];
        $upload = new \UploadFile();
        $upload->int_max_size = 31457280;
        $upload->arr_allow_exts = array('mp3', 'wav', 'wma', 'm4a');
        $upload->str_save_path = $path;
        $arr_rs = $upload->upload($file);
        if ($arr_rs['int_code'] == 1) {
            $filePath = '/'.$path . $arr_rs['arr_data']['arr_data'][0]['savename'];
            $res['code'] = 1;
            $res['message'] = '上传成功';
            $res['path'] = $filePath;
            $res['filename'] = $file['name'];
        } else {
            $message = '上传失败';
            if($arr_rs['arr_data']['int_error']==8){
                $message = '文件大小超过限制';
            }elseif($arr_rs['arr_data']['int_error']==10){
                $message = '文件格式不正确';
            }
            $res['code'] = 0;
            $res['message'] = $message;
        }
        die(json_encode($res));
    }

    public function actionUpImage()
    {
        $path = 'Upload/images/classes/';
        if(empty($_FILES['file'])){
            die(json_encode(['code'=>0,'message'=>'请选择文件']));
        }
        $file = $_FILES['file'];
        $upload = new \UploadFile();
        $upload->int_max_size = 3145728;
        $upload->arr_allow_exts = array('gif', 'jpg', 'jpeg', 'bmp', 'png');
        $upload->str_save_path = $path;
        $arr_rs = $upload->upload($file);
        if ($arr_rs['int_code'] == 1) {
            $filePath = '/'.$path . $arr_rs['arr_data']['arr_data'][0]['savename'];
            $res['code'] = 1;
            $res['message'] = '上传成功';
            $res['path'] = $filePath;
            $res['filename'] = $file['name'];
        } else {
            $message = '上传失败';
            if($arr_rs['arr_data']['int_error']==8){
                $message = '文件大小超过限制';
            }elseif($arr_rs['arr_data']['int_error']==10){
                $message = '文件格式不正确';
            }
            $res['code'] = 0;
            $res['message'] = $message;
        }
        die(json_encode($res));
    }

    public function actionAddTag(){
        $name = addslashes(trim(Yii::$app->request->post('name')));
        $id = Yii::$app->request->post('id');
        $type = Yii::$app->request->post('type');
        $id = $id?$id:0;
        if(!trim($name)){
            die(json_encode(['code'=>0,'message'=>'请填写标签名称']));
        }
        if(mb_strlen($name)>5){
            die(json_encode(['code'=>0,'message'=>'标签名称不能大于5个字符']));
        }
        $check = IeltsWriteTag::find()->where("name='$name' and id!=$id")->asArray()->one();
        if($check){
            die(json_encode(['code'=>0,'message'=>'该标签已添加']));
        }
        if($type==1){
            die(json_encode(['code'=>1,'message'=>'success']));
        }
        if(!$id){
            $num = IeltsWriteTag::find()->count();
            if($num>=4){
                die(json_encode(['code'=>0,'message'=>'最多只能添加4个']));
            }
        }
        if($id){
            $model = IeltsWriteTag::findOne($id);
            $model->name = $name;
            $res = $model->save();
        }else{
            $model = new IeltsWriteTag();
            $model->name = $name;
            $res = $model->save();
            $id = $model->primaryKey;
        }
        if($res){
            die(json_encode(['code'=>1,'message'=>'保存成功','id'=>$id]));
        }else{
            die(json_encode(['code'=>0,'message'=>'保存失败']));
        }
    }

    public function actionDeleteTag(){
        $id = Yii::$app->request->post('id');
        $res = IeltsWriteTag::deleteAll("id=$id");
        if($res){
            die(json_encode(['code'=>1,'message'=>'删除成功']));
        }else{
            die(json_encode(['code'=>0,'message'=>'删除失败']));
        }
    }
}